package com.zwj.config;//package xyz.wjwnya.training_manage_system.config;

import com.zwj.interceptor.Interceptor;
import com.zwj.mapper.UserMapper;
import com.zwj.utils.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.nio.file.Path;
import java.nio.file.Paths;

@Configuration
class MyInterceptorConfig extends WebMvcConfigurationSupport {
    private final UserMapper userMapper;
    private final Path imagePath;
    @Autowired
    MyInterceptorConfig(UserMapper userMapper ,FileProperties fileProperties) {
        this.userMapper = userMapper;
        this.imagePath = Paths.get(fileProperties.getImage()).toAbsolutePath().normalize();
    }
    @Bean
    Interceptor Interceptor() {
        return new Interceptor(userMapper);
    }

    /*拦截器*/
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(Interceptor()).addPathPatterns("/**")
                .excludePathPatterns("/", "/user/login","/getKaptcha","/getCodeId","/image/**")
                .excludePathPatterns("/swagger-resources/**","/doc.html","/webjars/**", "/v2/**", "/swagger-ui.html/**","/favicon.ico");
    }
    /**
     * 静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //在windows环境下的图片存放资源路径
//        String os = System.getProperty("os.name");
//        if (os.toLowerCase().startsWith("win")) {  //windows系统
//            //第一个方法设置访问路径前缀，第二个方法设置资源路径
//            registry.addResourceHandler("/image/**").
//                    addResourceLocations("file:"+imagePath);
//        }else{//linux系统
            registry.addResourceHandler("/image/**").
                    addResourceLocations("file:" + FileUtils.mkdirshead(new ApplicationHome(getClass()),"/image/"));
//                     addResourceLocations("file:"+imagePath);
//        }
        if (!registry.hasMappingForPattern("/doc.html")) {
            registry.addResourceHandler("/doc.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
        }
        if (!registry.hasMappingForPattern("/webjars/**")) {
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
        super.addResourceHandlers(registry);
    }
    /*跨越*/
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")/*设置允许跨域的路径*/
                .allowedOrigins("*")/*设置允许跨域的域名*/
                .allowCredentials(true)/*设置允许的证书*/
                .allowedMethods("GET","POST","DELETE","PUT","OPTIONS")/*设置允许跨域的方法*/
                .maxAge(7200);/*设置跨域的最大时间*/
    }
}